Skip to content

Add booleans and strings to JsValue in the C FFI - #5693

Open
jasonmilad wants to merge 2 commits into
boa-dev:mainfrom
jasonmilad:ffi-value-bool-string
Open

jasonmilad wants to merge 2 commits into
boa-dev:mainfrom
jasonmilad:ffi-value-bool-string

Conversation

@jasonmilad

Copy link
Copy Markdown
Contributor

This Pull Request is related to #332. It follows #5618 (and is independent of #5689) by adding boolean and UTF-8 string support to JsValue, so C callers can construct and read those primitives without needing a context.

It changes the following:

  • Added boa_JsValue_from_boolean and boa_JsValue_as_boolean. as_boolean does not coerce truthy or falsy non-boolean values.
  • Added boa_JsValue_from_string, which validates UTF-8 and copies the input DiplomatStringView using its explicit byte length (including any '\0' bytes), returning NULL on malformed UTF-8.
  • Added boa_JsValue_as_string and boa_JsValue_as_string_lossy, encoding Boa strings as UTF-8 into a caller-provided DiplomatWrite buffer without coercion:
    • as_string returns a JsStringError (NotAString or InvalidEncoding for unpaired UTF-16 surrogates) without writing to the buffer on failure.
    • as_string_lossy replaces unpaired surrogates with U+FFFD and only fails on non-strings.
    • Both write the full UTF-8 string in a single write_str call so fixed buffers fail cleanly on overflow and diplomat_buffer_write_create grows at most once.
  • Regenerated the C headers in ffi/boa_ffi/bindings/c/ with diplomat-gen.
  • Added Rust unit tests (covering non-coercion, Latin1/UTF-16/NUL round trips, invalid UTF-8, unpaired surrogates, and engine-produced string slices), expanded the C11 tests (tests/c/value.c and tests/c/error.c), and updated ffi/boa_ffi/README.md.

@github-actions github-actions Bot added the Waiting On Review Waiting on reviews from the maintainers label Nov 25, 2026
@github-actions github-actions Bot added this to the v0.23 milestone Nov 25, 2026
@github-actions github-actions Bot added C-Documentation update documentation C-Tests Issues and PRs related to the tests. C-FFI Foreign Function Interface, to be able to use Boa from a different language, such as C labels Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Test262 conformance changes

Test result main count PR count difference
Total 53,578 53,578 0
Passed 51,457 51,457 0
Ignored 1,629 1,629 0
Failed 492 492 0
Panics 0 0 0
Conformance 96.04% 96.04% 0.00%

Tested main commit: 2d8a534c683d386144d202e88e642c7e09382e01
Tested PR commit: 0e3c3b63e5e4168bae371d8a50b04086f8d599c6
Compare commits: 2d8a534...0e3c3b6

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 63.06%. Comparing base (6ddc2b4) to head (0e3c3b6).
⚠️ Report is 1090 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5693       +/-   ##
===========================================
+ Coverage   47.24%   63.06%   +15.82%     
===========================================
  Files         476      538       +62     
  Lines       46892    58600    +11708     
===========================================
+ Hits        22154    36959    +14805     
+ Misses      24738    21641     -3097     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jasonmilad
jasonmilad marked this pull request as ready for review November 25, 2026 22:48
@jasonmilad
jasonmilad requested a review from a team as a code owner November 25, 2026 22:48

@abdulhamid999 abdulhamid999 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed ffi/boa_ffi/src/lib.rs, ffi/boa_ffi/README.md, generated headers in ffi/boa_ffi/bindings/c/, and the Rust/C tests in tests/c/ (static inspection; not compiled locally).

Findings

  • Non-coercive boolean & string APIs: from_boolean / as_boolean and from_string / as_string / as_string_lossy cleanly avoid JS type coercion, preserve embedded NUL bytes via explicit slice length (DiplomatStr), reject invalid UTF-8 with NULL, and separate JsStringError::NotAString from JsStringError::InvalidEncoding (unpaired UTF-16 surrogates) while providing lossy U+FFFD replacement in as_string_lossy.
  • Test coverage & memory/lifetime safety: Rust tests and C tests (tests/c/value.c, tests/c/error.c) thoroughly exercise Latin1/UTF-16/emoji/NUL round trips, caller-buffer mutation after from_string, NULL data with length 0, fixed vs growable DiplomatWrite buffers, and primitive lifetime after BoaContext destruction.
  • Docstring completeness nit (ffi/boa_ffi/src/lib.rs): README.md clearly documents checking write.grow_failed on fixed stack buffers (diplomat_simple_write), and tests/c/value.c verifies that small_res.is_ok remains true while small_write.grow_failed is true and len == 0. However, the doc comments on JsValue::as_string and JsValue::as_string_lossy in src/lib.rs only mention checking is_ok (not write.grow_failed). Adding a brief note there about write.grow_failed for fixed stack buffers would keep the Rust/header docs aligned with README.md.

Clean addition to the FFI surface overall!

@jasonmilad

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Good catch, I added a note about checking write.grow_failed for fixed stack buffers to the doc comments on as_string and as_string_lossy so they stay aligned with README.md.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Documentation update documentation C-FFI Foreign Function Interface, to be able to use Boa from a different language, such as C C-Tests Issues and PRs related to the tests. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants